var myChart;
var dataArray = [];
var data0;
var option;
var time;
var open;
var close;
var hight;
var low;

//处理历史数据
//时间，开盘，收盘，最高价，最低价
function managerHistory(data){
    var array = data.split(";");
    for(var i = 0;i<array.length-1;i++){
        var cellArray = [];
        for(var k = 0;k<array[i].split(",").length;k++){
            if(k == 0){//日期
                cellArray.push(array[i].split(",")[k]);
            }else{
                cellArray.push(parseFloat(array[i].split(",")[k]));
            }
        }
        //不同时间戳
        if(dataArray.length<=0 || dataArray[dataArray.length - 1][0]!=cellArray[0]){
            dataArray.push(cellArray);
        }else{
            managerSameTimeData(cellArray);
        }
    }
}

//处理实时数据
function setData(data) {
    var lastDataArray = [];
    for(var i = 0;i<data.split(",").length;i++){
        if(i == 0){
            lastDataArray.push(data.split(",")[i]);
        }else{
            lastDataArray.push(parseFloat(data.split(",")[i]));
        }
    }
    //不同时间戳
    if(lastDataArray[0]!=data0.categoryData[data0.categoryData.length - 1]){
        lastDataArray[2] = lastDataArray[1];
        lastDataArray[3] = lastDataArray[1];
        lastDataArray[4] = lastDataArray[1];
        dataArray.push(lastDataArray);
    }else{
        managerSameTimeData(lastDataArray);
    }
}

//当数据为和上一天数据为同意时间戳
function managerSameTimeData(cellArray) {
    time = dataArray[dataArray.length - 1][0];
    open = dataArray[dataArray.length - 1][1];
    close = cellArray[1];
    hight = dataArray[dataArray.length - 1][3];
    low = dataArray[dataArray.length - 1][4];
    if(hight < cellArray[1]){
        hight = cellArray[1];
    }
    if(low > cellArray[1]){
        low = cellArray[1];
    }

    dataArray[dataArray.length - 1] = [time,open,close,hight,low];
}

//把数据转换为对象
function splitData(rawData) {
    var categoryData = [];
    var values = [];
    for (var i = 0; i < rawData.length; i++) {
        categoryData.push(rawData[i][0]);

    }

    for(var j = 0;j<rawData.length;j++){
        var valueCellArray = [];
        for(var k = 1;k<rawData[j].length;k++){
            valueCellArray.push(rawData[j][k]);
        }
        values.push(valueCellArray);
    }
    return {
        categoryData: categoryData,
        values: values
    };
}

function calculateMA(dayCount) {
    var result = [];
    for (var i = 0, len = data0.values.length; i < len; i++) {
        if (i < dayCount) {
            result.push('-');
            continue;
        }
        var sum = 0;
        for (var j = 0; j < dayCount; j++) {
            sum += data0.values[i - j][1];
        }
        result.push(sum / dayCount);
    }
    return result;
}

function getOption () {
    data0 = splitData(dataArray);
    option = {
        title: {
            text: '上证指数',
            left: 0
        },
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'cross'
            }
        },
        legend: {
            data: ['日K', 'MA5', 'MA10', 'MA20', 'MA30']
        },
        grid: {
            left: '10%',
            right: '10%',
            bottom: '15%'
        },
        xAxis: {
            type: 'category',
            data: data0.categoryData,
            scale: true,
            boundaryGap : false,
            axisLine: {onZero: false},
            splitLine: {show: false},
            splitNumber: 20,
            min: 'dataMin',
            max: 'dataMax'
        },
        yAxis: {
            scale: true,
            splitArea: {
                show: true
            }
        },
        dataZoom: [
            {
                type: 'inside',
                start: 50,
                end: 100
            },
            {
                show: true,
                type: 'slider',
                y: '90%',
                start: 50,
                end: 100
            }
        ],
        series: [
            {
                name: '日K',
                type: 'candlestick',
                data: data0.values,
                markPoint: {
                    label: {
                        normal: {
                            formatter: function (param) {
                                return param != null ? Math.round(param.value) : '';
                            }
                        }
                    },
                    data: [
                        {
                            name: 'XX标点',
                            coord: ['2013/5/31', 2300],
                            value: 2300,
                            itemStyle: {
                                normal: {color: 'rgb(41,60,85)'}
                            }
                        },
                        {
                            name: 'highest value',
                            type: 'max',
                            valueDim: 'highest'
                        },
                        {
                            name: 'lowest value',
                            type: 'min',
                            valueDim: 'lowest'
                        },
                        {
                            name: 'average value on close',
                            type: 'average',
                            valueDim: 'close'
                        }
                    ],
                    tooltip: {
                        formatter: function (param) {
                            return param.name + '<br>' + (param.data.coord || '');
                        }
                    }
                },
                markLine: {
                    symbol: ['none', 'none'],
                    data: [
                        [
                            {
                                name: 'from lowest to highest',
                                type: 'min',
                                valueDim: 'lowest',
                                symbol: 'circle',
                                symbolSize: 10,
                                label: {
                                    normal: {show: false},
                                    emphasis: {show: false}
                                }
                            },
                            {
                                type: 'max',
                                valueDim: 'highest',
                                symbol: 'circle',
                                symbolSize: 10,
                                label: {
                                    normal: {show: false},
                                    emphasis: {show: false}
                                }
                            }
                        ],
                        {
                            name: 'min line on close',
                            type: 'min',
                            valueDim: 'close'
                        },
                        {
                            name: 'max line on close',
                            type: 'max',
                            valueDim: 'close'
                        }
                    ]
                }
            },
            {
                name: 'MA5',
                type: 'line',
                data: calculateMA(5),
                smooth: true,
                lineStyle: {
                    normal: {opacity: 0.5}
                }
            },
            {
                name: 'MA10',
                type: 'line',
                data: calculateMA(10),
                smooth: true,
                lineStyle: {
                    normal: {opacity: 0.5}
                }
            },
            {
                name: 'MA20',
                type: 'line',
                data: calculateMA(20),
                smooth: true,
                lineStyle: {
                    normal: {opacity: 0.5}
                }
            },
            {
                name: 'MA30',
                type: 'line',
                data: calculateMA(30),
                smooth: true,
                lineStyle: {
                    normal: {opacity: 0.5}
                }
            },

        ]
    };
    return option;
}